package com.lianbang.pharmacy.module.main

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.view.Menu
import android.view.MenuItem
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager

import com.lianbang.pharmacy.R
import cn.ubest.mvvmcore.base.BaseActivity
import cn.ubest.mvvmcore.utils.GlideUtils
import cn.ubest.mvvmcore.utils.ResourcesUtils
import cn.ubest.mvvmcore.utils.SpanUtils
import cn.ubest.mvvmcore.utils.ToastUtils
import com.lianbang.pharmacy.adapter.MedicineDetailCompanyListAdapter
import com.lianbang.pharmacy.databinding.ActivitySupplierDetailBinding
import cn.ubest.mvvmcore.widget.ListDivider
import com.lianbang.pharmacy.bean.MedicineInfoBean
import com.lianbang.pharmacy.vm.MedicineVM
import com.lianbang.pharmacy.vm.SupplierVM
import kotlinx.android.synthetic.main.activity_supplier_detail.*

class MedicineSupplierActivity : BaseActivity<SupplierVM, ActivitySupplierDetailBinding>() {

    private val mAdapter by lazy {
        MedicineDetailCompanyListAdapter()
    }

    private val mId by lazy {
        intent.getIntExtra("id", -1)
    }

    companion object {
        fun start(context: Context?, id: Int) {
            context?.also { c ->
                val starter = Intent(c, MedicineSupplierActivity::class.java)
                if (c !is Activity) {
                    starter.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                }
                starter.putExtra("id", id)
                c.startActivity(starter)
            }
        }
    }

    override fun initLayout(): Int {
        return R.layout.activity_supplier_detail

    }

    override fun initData(intent: Intent) {
        mViewModel.mMedicineLiveData.observe(this, Observer {
            initMedicineInfo(it)
        })
        mViewModel.mSupplierListLiveData.observe(this, Observer {
            mAdapter.replaceData(it)
        })
    }

    override fun initView() {
        rv_list.layoutManager = LinearLayoutManager(this)
        rv_list.addItemDecoration(
            ListDivider.Build()
                .setHeight(R.dimen.dp_1)
                .setMarginLeft(R.dimen.dp_20)
                .setMarinRight(R.dimen.dp_20)
                .setColor(R.color.C_F3F5F9).build()
        )
        rv_list.adapter = mAdapter
    }

    private fun initMedicineInfo(bean: MedicineInfoBean) {
        tv_standard.text = SpanUtils().append("产品规格：")
            .setForegroundColor(ResourcesUtils.getColor("#9B9B9B"))
            .append("0.5x12片x2板/盒")
            .setForegroundColor(ResourcesUtils.getColor("#4A4A4A"))
            .create()
        tv_type.text = SpanUtils().append("剂型：")
            .setForegroundColor(ResourcesUtils.getColor("#9B9B9B"))
            .append("薄膜衣片剂")
            .setForegroundColor(ResourcesUtils.getColor("#4A4A4A"))
            .create()
        tv_number.text = SpanUtils().append("批准文号：")
            .setForegroundColor(ResourcesUtils.getColor("#9B9B9B"))
            .append("国药准字B20050066")
            .setForegroundColor(ResourcesUtils.getColor("#4A4A4A"))
            .create()
        tv_company.text = SpanUtils().append("生产企业：")
            .setForegroundColor(ResourcesUtils.getColor("#9B9B9B"))
            .append("漳州片仔癀药业股份有限公司")
            .setForegroundColor(ResourcesUtils.getColor("#4A4A4A"))
            .create()

        tv_company_count.text = SpanUtils().append("58")
            .setForegroundColor(ResourcesUtils.getColor("#5671FB"))
            .setBold()
            .append("个商家报价")
            .setForegroundColor(ResourcesUtils.getColor("#4A4A4A"))
            .create()

        GlideUtils.display(this, bean.product.image, iv_img)
        tv_title.text = bean.product.store_name
        tv_desc.initWidth(resources.displayMetrics.widthPixels - ResourcesUtils.getDimens(R.dimen.dp_40))
        tv_desc.maxLines = 3
        tv_desc.setOpenSuffixColor(ResourcesUtils.getColor("#5671FB"))
        tv_desc.setCloseSuffixColor(ResourcesUtils.getColor("#5671FB"))
        tv_desc.setCloseInNewLine(false)
        tv_desc.setOriginalText(bean.product.store_info)

    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.menu_supplier_detail, menu)
        return super.onCreateOptionsMenu(menu)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.btn_search -> ToastUtils.toast("搜索")
            R.id.btn_share -> ToastUtils.toast("分享")
        }
        return super.onOptionsItemSelected(item)
    }


    override fun loadData() {
        mViewModel.medicineInfo(mId)
        mViewModel.medicineSupplierList(mId)
    }
}
